﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace IOExcel
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fbd = new FolderBrowserDialog();//创建文件夹选择框
            if(fbd.ShowDialog() == DialogResult.OK)
            {
                string folderPath = fbd.SelectedPath;//获取选择的文件夹的路径
               string[] folders = Directory.GetDirectories(folderPath);//子文件夹列表
                listBox1.Items.Clear();//清空列表
                foreach (string folder in folders)
                {
                    //获取文件夹信息
                    DirectoryInfo di = new DirectoryInfo(folder);
                    listBox1.Items.Add(di.FullName);
                }
            }
        }

        //列表框选择目录
        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string folderPath = listBox1.SelectedItem.ToString();//获取选中的目录路径
            DirectoryInfo di = new DirectoryInfo(folderPath);//创建目录信息对象
            FileInfo[] fileInfo = di.GetFiles();
            listBox2.Items.Clear();
            foreach (FileInfo fi in fileInfo)
            {
                listBox2.Items.Add(fi.Name);
            }
        }

        /// <summary>
        /// 遍历图片文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            string path = @"C:\Users\33159\Pictures\Screenshots";
            string fileName = "05.jpg";
            string  fileNameNew = Path.Combine(path, fileName);//C:\Users\33159\Pictures\Screenshots\05.jpg
            if (Directory.Exists(path))//判断目录是否存在
            {
                string[] fileInfo = Directory.GetFiles(path);//获取目录下的所有文件
                int i = 0;
                foreach (string fi in fileInfo)
                {
                    string ext = Path.GetExtension(fi).ToLower();//获取文件扩展名
                    if(ext == ".png" || ext == ".jpg")
                    {
                        Image img = Image.FromFile(fi);//创建图片对象
                        imageList1.Images.Add(img);
                        ListViewItem li = new ListViewItem();
                        li.Text = Path.GetFileName(fi);
                        li.ImageIndex = i;
                        listView1.Items.Add(li);
                        i++;
                    }
                }
            }
            
        }
    }
}
